﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using WpfApp5_StackPanel.Model;

namespace WpfApp5_StackPanel
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        private delegate void SetTimeDelegate(DateTime time);

        private Timer myTimer;

        // 1. 声明依赖属性变量（总是public static的）
        public static DependencyProperty TimeProperty;
        // 1. 声明CLR属性，使用DependencyObject的Set和Get方法来设定或获取TimeProperty的值
        public DateTime Time
        {
            get
            {
                return (DateTime)GetValue(TimeProperty);
            }
            set
            {
                SetValue(TimeProperty, value);
            }
        }

        // 2. 注册相关属性，定义静态构造函数
        static Window1()
        {
            // 与依赖属性相关的元数据
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
            metadata.Inherits = true; // TimerProperty可以向子元素传递
            metadata.DefaultValue = DateTime.Now; // 设置初始值
            metadata.AffectsMeasure = true; // TimerProperty的改变会影响UI元素的尺寸
            metadata.PropertyChangedCallback += OnTimerPropertyChanged; // TimerProperty的值改变时，事件处理程序

            TimeProperty = DependencyProperty.Register(
                "Timer",
                typeof(DateTime),
                typeof(Window1),
                metadata,
                ValidateTimeValue);

        }

        private static void OnTimerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private static bool ValidateTimeValue(object value)
        {
            DateTime dt = (DateTime)value;
            if (dt.Year > 1990 && dt.Year < 2200)
            {
                return true;
            }
            return false;
        }

        public Window1() : base()
        {
            InitializeComponent();
            // 每秒钟调用RefreshTime方法
            myTimer = new Timer(
                new TimerCallback(RefreshTime),
                new AutoResetEvent(false), 0, 1000);

            UseListView();
        }

        private void RefreshTime(object state)
        {
            // 由于RefreshTime和WPF的UI并不在一个线程内，故需要使用Window类的继承树中的DispatcherObject对象
            // 中的Dispatcher.BeginInvoke方法，把此任务委托给SetTimerProperty函数来完成。
            this.Dispatcher.BeginInvoke(
                DispatcherPriority.Send,
                new SetTimeDelegate(SetTimerProperty),
                DateTime.Now);
        }

        private void SetTimerProperty(DateTime time)
        {
            this.Time = time;
        }


        private ObservableCollection<PersonalInfo> personalInfoList = new ObservableCollection<PersonalInfo>();
        public ObservableCollection<PersonalInfo> PersonalInfoList
        {
            get
            {
                return personalInfoList;
            }
        }
        public void UseListView()
        {
            personalInfoList.Add(new PersonalInfo("程俊", 8, "4338819", "chenjun@hotmail.com"));
            personalInfoList.Add(new PersonalInfo("张珊", 12, "6566665", "zhangshan@hotmail.com"));
            personalInfoList.Add(new PersonalInfo("陶铸", 7, "65765675", "taozhu@hotmail.com"));
            personalInfoList.Add(new PersonalInfo("李伟光", 25, "8877172", "liweiguang@hotmail.com"));
            personalInfoList.Add(new PersonalInfo("刘表", 3, "9076112", "liubiao@hotmail.com"));
        }
        private void AddRow_Click(object sender, RoutedEventArgs e)
        {
            personalInfoList.Add(new PersonalInfo("林晓霞", 1, "3452354", "xiaoshuanglin@hotmail.com"));

        }
    }
}
